package com.example.rbacmanage.controller.system;

import com.example.rbacmanage.annotation.LogAnnotation;
import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.system.Dept;
import com.example.rbacmanage.service.system.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/system/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping(value = "/tree")
    @PreAuthorize("hasAnyAuthority('system:dept:list')")
    public Result getTree(Dept dept) {
        return Result.success(deptService.getTree(dept));
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id){
        return Result.success(deptService.getById(id));
    }

    @PostMapping("/add")
    @LogAnnotation(module = "部门模块",operation = "新增部门")
    @PreAuthorize("hasAnyAuthority('system:dept:add')")
    public Result add(@RequestBody Dept dept){
        deptService.save(dept);
        return Result.success();
    }

    @PutMapping("/update")
    @LogAnnotation(module = "部门模块",operation = "修改部门")
    @PreAuthorize("hasAnyAuthority('system:dept:edit')")
    public Result edit(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }

    @DeleteMapping("/remove")
    @LogAnnotation(module = "部门模块",operation = "删除部门")
    @PreAuthorize("hasAnyAuthority('system:dept:delete')")
    public Result remove(@RequestBody Long[] ids){
        deptService.remove(ids);
        return Result.success();
    }
}
